import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)

import VueResource from 'vue-resource'
Vue.use(VueResource)

import App from './app.vue'

import './lib/mui/css/mui.css'
import './lib/mui/css/icons-extra.css'

import './css/style.css'

import router from './router.js'

Vue.http.options.root = 'http://www.liulongbin.top:3005/api';
Vue.http.options.emulateJSON = true;


import MintUI from 'mint-ui'
import 'mint-ui/lib/style.css'
Vue.use(MintUI)

import moment from 'moment'
import VuePreview from 'vue-preview'
Vue.use(VuePreview)

import Vuex from 'vuex'
Vue.use(Vuex)
const car =JSON.parse(localStorage.getItem('car')||'[]')
const store=new Vuex.Store({
  state:{
    // 获取state中的数据：this.$store.state.***
    car:car

  },
  mutations:{
    // 修改数据：this.$store.commit('方法名'，唯一的参数)
    add(state,goodsInfo){
      let flag=false
      state.car.some(item=>{
        if(item.id==goodsInfo.id){
          item.count+=parseInt(goodsInfo.count) ;
          flag=true;
          return true
        }
      })
      if(!flag){
        state.car.push(goodsInfo)
      }
      localStorage.setItem('car',JSON.stringify(state.car))
    }
  },
  getters:{
    // 获取getters中数据：this.$store.getters.***
    getAllCount(state){
      let c=0;
      state.car.forEach(item=>{
        c+=item.count
      })
      return c
    }
  }
})

Vue.filter('dateFormate',function(dataStr,pattern="YYYY-MM-DD HH:mm:ss"){
  return moment(dataStr).format(pattern)
})

const vm=new Vue({
  el:'#app',
  render:c=>c(App),
  router,
  store
})